home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / MeshWriterTest / modules / landscape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  4.8 KB  |  158 lines

  1. /*
  2. **      $VER: landscape.c 1.00 (27.3.1999)
  3. **
  4. **      Creation date     : 27.3.1999
  5. **
  6. **      Description       :
  7. **         MeshWriter testprogram shape module.
  8. **
  9. **
  10. **      Written by Stephan Bielmann
  11. **
  12. */
  13.  
  14. /*************************** Includes *******************************/
  15.  
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. #ifndef WITHMWLLIB
  20. #include "//meshlib.h"
  21. #else
  22. #include <meshwriter/meshwriter.h>
  23. #include <pragma/meshwriter_lib.h>
  24. #endif
  25.  
  26. /**************************** Defines *******************************/
  27.  
  28. #define GRID_SIZE     32
  29.  
  30. /*********************** Type definitions ***************************/
  31.  
  32. /********************** Private functions ***************************/
  33.  
  34. static TOCLFloat randomHeight (FLOAT range)
  35. {
  36.   return (TOCLFloat) (range * ((TOCLFloat) (2.0 * rand () / RAND_MAX) - 1.0));
  37. }
  38.  
  39. /********************** Public functions ****************************/
  40.  
  41. /********************************************************************\
  42. *                                                                    *
  43. * Name         : landscape                                           *
  44. *                                                                    *
  45. * Description  : Draws a landscape.                                  *
  46. *                                                                    *
  47. * Arguments    : mesh    IN  : An initialized mesh handle.           *
  48. *                                                                    *
  49. * Comment      : This function is a "remake" of the Amiga-Magzin its *
  50. *                landscape sample code used in theyr Cyber-GL        *
  51. *                workshop.                                           *
  52. *                                                                    *
  53. \********************************************************************/
  54. void landscape(ULONG mesh) {
  55.   TOCLVertex grid[GRID_SIZE+1][GRID_SIZE+1];
  56.   TOCLColor color;
  57.   ULONG c[3];
  58.   int x, z;       /* x/z index */
  59.   int gridStep;   /* actual grid index distance */
  60.   int step;       /* actual generation step */
  61.   int seed,steps,i,j;
  62.   float maxHeight,minh,maxh;
  63.   int xl,xr,zl,zr;
  64.  
  65.   seed=rand();
  66.   steps=5;
  67.   maxHeight=5;
  68.  
  69.   MWLMeshMaterialAdd(mesh,&c[0]);
  70.   color.r=0,color.g=0,color.b=255;
  71.   MWLMeshMaterialDiffuseColorSet(mesh,c[0],&color);
  72.  
  73.   MWLMeshMaterialAdd(mesh,&c[1]);
  74.   color.r=0,color.g=255,color.b=0;
  75.   MWLMeshMaterialDiffuseColorSet(mesh,c[1],&color);
  76.  
  77.   MWLMeshMaterialAdd(mesh,&c[2]);
  78.   color.r=255,color.g=255,color.b=255;
  79.   MWLMeshMaterialDiffuseColorSet(mesh,c[2],&color);
  80.  
  81.   for(i=0;i<(GRID_SIZE+1);i++) {
  82.     for(j=0;j<(GRID_SIZE+1);j++) {
  83.       grid[i][j].x=i;
  84.       grid[i][j].y=j;
  85.       grid[i][j].z=0;
  86.     }
  87.   }
  88.  
  89.   srand (seed);
  90.   grid [        0][        0].z = randomHeight (maxHeight);
  91.   grid [GRID_SIZE][        0].z = randomHeight (maxHeight);
  92.   grid [        0][GRID_SIZE].z = randomHeight (maxHeight);
  93.   grid [GRID_SIZE][GRID_SIZE].z = randomHeight (maxHeight);
  94.  
  95.   gridStep = GRID_SIZE / 2;
  96.  
  97.   for (step = 1; step <= steps; step++) {
  98.     maxHeight /= 2.0;
  99.     for (x = gridStep / 2; x < GRID_SIZE; x += gridStep) {
  100.       xl = x - gridStep / 2;
  101.       xr = x + gridStep / 2;
  102.  
  103.       for (z = gridStep / 2; z < GRID_SIZE; z += gridStep) {
  104.         zl = z - gridStep / 2;
  105.         zr = z + gridStep / 2;
  106.  
  107.         grid [x][z].z = (grid [xl][zl].z +
  108.                          grid [xl][zr].z +
  109.                          grid [xr][zl].z +
  110.                          grid [xr][zr].z) / 4.0 + randomHeight (maxHeight);
  111.         grid [xl][z].z = (grid [xl][zl].z +
  112.                           grid [xl][zr].z) / 2.0 + randomHeight (maxHeight);
  113.         grid [x][zl].z = (grid [xl][zl].z +
  114.                           grid [xr][zl].z) / 2.0 + randomHeight (maxHeight);
  115.       }
  116.       grid [x][GRID_SIZE].z = (grid [xl][GRID_SIZE].z +
  117.                                grid [xr][GRID_SIZE].z) / 2.0 + randomHeight (maxHeight);
  118.  
  119.     }
  120.     for (z = gridStep / 2; z < GRID_SIZE; z += gridStep) {
  121.       grid [GRID_SIZE][z].z = (grid [GRID_SIZE][z - gridStep / 2].z +
  122.                                grid [GRID_SIZE][z + gridStep / 2].z) / 2.0 + randomHeight (maxHeight);
  123.     }
  124.     gridStep /= 2;
  125.   }
  126.  
  127.   maxh=-1000;minh=1000;
  128.   for(i=0;i<(GRID_SIZE);i++) {
  129.     for(j=0;j<(GRID_SIZE);j++) {
  130.         if(maxh<grid[i][j].z) maxh=grid[i][j].z;
  131.         if(minh>grid[i][i].z) minh=grid[i][j].z;
  132.     }
  133.   }
  134.  
  135.   for(i=0;i<(GRID_SIZE);i++) {
  136.     for(j=0;j<(GRID_SIZE);j++) {
  137.          ULONG col;
  138.         
  139.         if(minh<0) {
  140.             col=ULONG(((grid[i][j].z-minh))/((maxh-minh)/3));
  141.         } else {
  142.             col=ULONG(((grid[i][j].z+minh))/((maxh-minh)/3));
  143.         }
  144.         if(col>2)col=2;if(col<0)col=0;
  145.  
  146.         MWLMeshPolygonAdd(mesh,c[col]);
  147.         MWLMeshPolygonVertexAdd(mesh,&grid[i][j]);
  148.         MWLMeshPolygonVertexAdd(mesh,&grid[i+1][j]);
  149.         MWLMeshPolygonVertexAdd(mesh,&grid[i+1][j+1]);
  150.         MWLMeshPolygonVertexAdd(mesh,&grid[i][j+1]);
  151.     }
  152.   }
  153.  
  154.   MWLMeshNameSet(mesh,"Landscape");
  155. }
  156.  
  157. /************************* End of file ******************************/
  158.